﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

using System.Net;
using System.Net.Sockets;

namespace Tensop.Net.Http
{
    public class Downloader
    {
        static int RandomSeed;
        static Downloader()
        {
            var rng = new RNGCryptoServiceProvider ();
            byte[] temp = new byte[4];
            rng.GetBytes ( temp );
            RandomSeed = BitConverter.ToInt32 ( temp, 0 );
        }


        class PageDownload : IDisposable
        {
            const string                        UserAgent                   ="";
            const string                        Accept                      ="";
            const string                        AcceptLanguage              ="";
            const string                        KeepLive                    ="";




            string                              version;
            string                              targetSite;
            string                              cookie;
            string                              host;
            string                              method;
            string                              referer;
            Socket                              socket;
            IPEndPoint                          remote;
            HttpStatusCode                      errorCode;

            public event Action                 OnError;
            public event Action<string>         OnReceive;

            public PageDownload(string site, string cookie,string host,string referer,HttpMethod method,HttpVersion version)
            {
                try
                {
                    var ip = Dns.GetHostAddresses ( site );
                    var rnd = new Random ( RandomSeed );
                    remote = new IPEndPoint ( ip[rnd.Next ( 0, ip.Length - 1 )], 80 );//获得指定网址的IP地址
                    targetSite = site;
                    this.cookie = cookie;
                    this.host = host;
                    this.referer = referer;
                    this.method = method.ToString ();
                    this.version = version == HttpVersion.Http_1_0 ? "HTTP/1.0" : "HTTP/1.1";
                }
                catch
                {
                    errorCode = HttpStatusCode.ConnectionError;
                    OnError?.Invoke ();
                }
            }

            public void Start()
            {
                socket = new Socket ( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
                Task t = new Task ( () =>
                {
                    try
                    {

                    }
                    catch
                    {

                    }
                } );
                t.Start ();
            }


            public void Dispose()
            {

            }
        }






        internal Downloader()
        {

        }
    }
}
